const shop = require('../../../mixins/shop');
import config from "../../../utils/config";
const app = getApp();
const options = {

    /**
     * 页面的初始数据
     */
    data: {
        isEmpty: false,
        isAllRadio: false,
        isTouchMove: false,
        startX: 0,
        startY: 0,
        showDialog: false,
        cartGoods: [],
        maxNum: 0,
        amount: 0,
        vipAmount: 0,
        discountsAmount: 0,
        recommendList: [],
        page: 1,
        loadmoreShow: false,
        isLastPage: false,
        num: config.cart,
        cidx: 0,
        pid: 0,
        cid: 0,
    },
    calculateFun() {
        let that = this;
        let cartGoods = that.data.cartGoods;
        let maxNum = 0;
        let outNum = 0;
        let amount = 0.00;
        let vipAmount = 0.00;
        let discountsAmount = 0.00;
        let isAllRadio = false;
        for (let v of cartGoods) {
            if (v.is_check && v.state == 0) {
                maxNum += 1;
                amount = amount + parseFloat(v.price * v.num);
                vipAmount = vipAmount + parseFloat(v.vip_price * v.num);
            }
            if (v.state == 3) {
                outNum += 1;
            }
        }
        discountsAmount = amount.toFixed(2) - vipAmount.toFixed(2);
        amount = amount.toFixed(2);
        vipAmount = vipAmount.toFixed(2);
        discountsAmount = discountsAmount.toFixed(2);
        if (cartGoods.length == maxNum + outNum) {
            isAllRadio = true;
        }
        that.setData({
            maxNum: maxNum,
            amount: amount,
            vipAmount: vipAmount,
            discountsAmount: discountsAmount,
            isAllRadio: isAllRadio,
        })
    },
    onLoad: function (options) {
        this.getShopIndexRecommend();
    },
    onShow: function () {
        this.getShopUserCartList();
    },
    toPlaceOrder() {
        let that = this;
        if (that.data.maxNum > 0) {
            wx.navigateTo({
                url: '/pages/shop/place-order/place-order',
            })
        }
    },
    allCart(e) {
        let that = this;
        let isAllRadio = that.data.isAllRadio;
        if (isAllRadio) {
            isAllRadio = 0
        } else {
            isAllRadio = 1
        }
        that.getShopAllCheckCartGoods(isAllRadio);
    },
    radioGoods(e) {
        let that = this;
        let index = e.currentTarget.dataset.index;
        let pid = e.currentTarget.dataset.pid;
        let check = e.currentTarget.dataset.check;
        if (check) {
            check = 0;
        } else {
            check = 1;
        }
        that.data.cartGoods.forEach(function (v, i) {
            if (i == index) {
                v.is_check = !v.is_check;
            }
        })
        that.setData({
            cartGoods: that.data.cartGoods
        })
        that.getShopCheckCartGoods(pid, check)
    },
    onClickDialog(e) {
        let that = this;
        let idx = e.currentTarget.dataset.idx;
        that.setData({
            showDialog: true,
            cidx: idx,
            pid: that.data.cartGoods[idx].product_id
        })
    },
    deleteGoods(e) {
        let that = this;
        let cid = e.currentTarget.dataset.id;
        let idx = e.currentTarget.dataset.idx;
        wx.showModal({
            title: '提示',
            content: '是否确认要删除商品？',
            success(res) {
                if (res.confirm) {
                    let cartGoods = that.data.cartGoods;
                    cartGoods.splice(idx, 1);
                    that.setData({
                        cid: cid,
                        cartGoods: cartGoods
                    })
                    that.getShopDelCartGoods();
                }
            }
        })
    },
    deleteGoodsPp() {
        let that = this;
        let idx = that.data.cidx;
        let cid = that.data.cartGoods[idx].id;
        wx.showModal({
            title: '提示',
            content: '是否确认要删除商品？',
            success(res) {
                if (res.confirm) {
                    let cartGoods = that.data.cartGoods;
                    cartGoods.splice(idx, 1);
                    that.setData({
                        cid: cid,
                        cartGoods: cartGoods,
                        showDialog: false
                    })
                    that.getShopDelCartGoods();
                }
            }
        })
    },
    shareGoodsClick() {
        let that = this;
        let postsGoods = [];
        let goods = {};
        let idx = that.data.cidx;
        goods.id = that.data.cartGoods[idx].goods_id;
        goods.name = that.data.cartGoods[idx].goods_name;
        goods.pic = that.data.cartGoods[idx].goods_pic;
        postsGoods.push(goods);
        wx.setStorageSync('postsGoods', postsGoods);
        wx.navigateTo({
            url: '/pages/creat/index/creat',
        })
    },
    onClick() {
        this.setData({
            showDialog: false
        })
    },
    bindPickerChange: function (e) {
        let that = this;
        let idx = e.currentTarget.dataset.idx;
        let pid = e.currentTarget.dataset.pid;
        let value = e.detail.value;
        that.getShopSaveCartNum(pid, that.data.num[value], idx, value)
    },
    touchstart: function (e) {
        //开始触摸时 重置所有删除
        this.data.cartGoods.forEach(function (v, i) {
            if (v.isTouchMove) //只操作为true的
                v.isTouchMove = false;
        })
        this.setData({
            startX: e.changedTouches[0].clientX,
            startY: e.changedTouches[0].clientY,
            cartGoods: this.data.cartGoods
        })
    },
    //滑动事件处理
    touchmove: function (e) {
        var that = this,
            index = e.currentTarget.dataset.index, //当前索引
            startX = that.data.startX, //开始X坐标
            startY = that.data.startY, //开始Y坐标
            touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
            touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
            //获取滑动角度
            angle = that.angle({
                X: startX,
                Y: startY
            }, {
                X: touchMoveX,
                Y: touchMoveY
            });
        that.data.cartGoods.forEach(function (v, i) {
            v.isTouchMove = false
            //滑动超过30度角 return
            if (Math.abs(angle) > 30) return;
            if (i == index) {
                if (touchMoveX > startX) //右滑
                    v.isTouchMove = false
                else //左滑
                    v.isTouchMove = true
            }
        })
        //更新数据
        that.setData({
            cartGoods: that.data.cartGoods
        })
    },
    /**
     * 计算滑动角度
     * @param {Object} start 起点坐标
     * @param {Object} end 终点坐标
     */
    angle: function (start, end) {
        var _X = end.X - start.X,
            _Y = end.Y - start.Y
        //返回角度 /Math.atan()返回数字的反正切值
        return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
    },

    toGoodsDetails(e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/shop/goods-details/goods-details?id=' + id,
        })
    },

    toGoods() {
        wx.switchTab({
            url: '/pages/tabbar/shop/shop',
        })
    },

    onPullDownRefresh: function () {
        let that = this;
        that.setData({
            loadmoreShow: false,
            isLastPage: false,
            page: 1,
        });
        that.getShopUserCartList();
        that.getShopIndexRecommend();
        wx.stopPullDownRefresh();
    },

    onReachBottom: function () {
        let that = this;
        that.setData({
            loadmoreShow: true,
            page: that.data.page + 1
        });
        that.getShopIndexRecommend();
    },
    onShareAppMessage: function () {},
}

shop(options)
Page(options)